********************************************************************************
* Replication code
* "Are the effects of terrorism short-lived?"
* British Journal of Political Science
* Vincenzo Bove, Georgios Efthyvoulou & Harry Pickard
*
********************************************************************************

clear all

net install binsreg, from(https://raw.githubusercontent.com/nppackages/binsreg/master/stata) replace force
*net install ritest, from(https://raw.githubusercontent.com/simonheb/ritest/master/) replace

which binsreg // *! version 1.2 09-Oct-2022 
*** put replication_package/ado/bingsreg.ado in `c(sysdir_plus)' folder ***
di "`c(sysdir_plus)'b"
which binsreg // *! version 0.8 12-Oct-2021 

clear all
cap log close _all
est clear
set scheme s1color

* set the global called 'project' to the location of the 'replication_package' folder 
if "`c(username)'"=="nhp7115" {
	if "`c(machine_type)'"=="Mac (Apple Silicon)" {
		glo project "/Users/nhp7115/Dropbox/ESRC-SDAI Project (Shared)/Papers/CMS [PAPER]/1_BJPS/replication_package"
	}
	else {
		glo project "C:/Users/nhp7115/Dropbox/ESRC-SDAI Project (Shared)/Papers/CMS [PAPER]/1_BJPS/replication_package"
	}
	
}

*glo project "[...]"

cap mkdir "$project/OUTPUT"
glo output "$project/OUTPUT"

********************************************************************************

********************************************************************************
// Section 3.1

use "$project/replication_data_1"



qui { // Figure 1 : Binsreg

binsreg risk_terror01 rel_days, absorb(attack_gor) by(post) masspoints(off) dots(0,0) line(0,0) cb(0,0) ci(0,0) ///
bycolors(blue red) bysymbols(O O) ///	
xlabel(,nogrid nogextend) ylabel(,nogrid nogextend) ///
xline(0, lp(dash) lc(black)) xtitle("Days relative to attack") ytitle("`: var lab risk_terror01'") ///
legend(order(2 "Pre-attack" 6 "Post-attack" 8 "95% CI" 5 "95% CB") pos(6) row(1) region(lwidth(none))) name("risk_terror01", replace) fysize(100) fxsize(100)

hist rel_days if !missing(risk_terror01), freq fc(black%25) ls(none) width(1) barwidth(1) xtitle("") name("freq", replace) ylabel(500 1000) fysize(35) fxsize(100)

gr combine risk_terror01 freq, row(2)
gr export "$output/fig_1.png", as(png) width(4000) replace


}



qui { // Figure 2 : Regs & RI

loc i=1
foreach y of varl risk_terror01 {
	
	** REGS
	est clear
	eststo: areg `y' vs_run, r absorb(attack_gor) cluster(attack_gor)
	eststo: areg `y' s_run, r absorb(attack_gor) cluster(attack_gor)
	eststo: areg `y' m_run, r absorb(attack_gor) cluster(attack_gor)

	coefplot ///
	(est1, keep(*run*) mc(purple) ciopts(lc(purple purple) lw(thick thin))) ///
	(est2, keep(*run*) mc(green) ciopts(lc(green green) lw(thick thin))) ///
	(est3, keep(*run*) mc(navy) ciopts(lc(navy navy) lw(thick thin))) ///
	, vert yline(0, lc(red)) ///
	ytitle("Treatment effects") xtitle("Time frame") ///
	xlabel(,nogrid nogextend) ylabel(-0.1(0.1)0.5, nogrid nogextend) ///
	plotregion(lc(gs8)) ///
	levels(90 95) msize(large) ///
	legend(off) name(reg_`y', replace)

	** RI
	foreach x in vs_run s_run m_run {	
		
		tempfile tmp_`x'
			
		qui areg `y' `x', r absorb(attack_gor) cluster(attack_gor)
		loc beta_`x'=_b[`x']
		
		ritest `x' _b[`x'], seed(1234) reps(1000) strata(attack_gor) saving(`tmp_`x'') ///
		: areg `y' `x', r absorb(attack_gor) cluster(attack_gor)
		
		loc `x'_pcn=(r(p)[1,1])*100
		
		preserve
		
		use `tmp_`x'', clear
		ren _pm_1 `x'
		kdensity `x'
		tempfile tmp`i'
		save `tmp`i''
		
		restore
		
		loc i=`i'+1
		
	}
				
preserve

use `tmp1', clear
merge 1:1 _n using `tmp2', nogen
merge 1:1 _n using `tmp3', nogen

twoway ///
(kdensity vs_run, recast(area) lc(purple) color(purple%30) text(35 `beta_vs_run' "`vs_run_pcn'%", color(purple) place(w) orient(vertical))) ///
(kdensity s_run, recast(area) lc(green) color(green%30) text(35 `beta_s_run' "`s_run_pcn'%", color(green) place(w) orient(vertical))) ///
(kdensity m_run, recast(area) lc(navy) color(navy%30) text(35 `beta_m_run' "`m_run_pcn'%", color(navy) place(w) orient(vertical))) ///
,  xlabel(-0.4(0.2)0.4, nogrid nogextend) ylabel(,nogrid nogextend) ///
plotregion(lc(gs8)) ///
xline(`beta_vs_run', lc(purple)) ///
xline(`beta_s_run', lc(green)) ///
xline(`beta_m_run', lc(navy)) ///
xtitle("Permutation effect estimates") ytitle("Density") ///
legend(off order(1 "Very short run" 2 "Short run" 3 "Medium run") pos(6) row(1)) ///
name(ri_`y', replace)

gr combine reg_`y' ri_`y'
gr export "$output/fig_2.png", as(png) width(4000) replace

restore

}

}



qui { // Figure 3 : Regs emotions - bad

est clear
foreach y of varl emo_terror_1 emo_terror_3 emo_terror_5 emo_terror_7 emo_terror_bad {
foreach x in vs_run s_run m_run {

	eststo: areg `y' `x', r absorb(attack_gor) cluster(attack_gor)
}
}
 
coefplot ///
(est1, offset(-0.2) keep(*run*) rename(vs_run="Anger") mc(purple) ciopts(lc(purple purple) lw(thick thin))) ///
(est2, offset(0)    keep(*run*) rename(s_run="Anger") mc(green) ciopts(lc(green green) lw(thick thin))) ///
(est3, offset(0.2)  keep(*run*) rename(m_run="Anger") mc(navy) ciopts(lc(navy navy) lw(thick thin))) ///
(est4, offset(-0.2) keep(*run*) rename(vs_run="Disgust") mc(purple) ciopts(lc(purple purple) lw(thick thin))) ///
(est5, offset(0)    keep(*run*) rename(s_run="Disgust") mc(green) ciopts(lc(green green) lw(thick thin))) ///
(est6, offset(0.2)  keep(*run*) rename(m_run="Disgust") mc(navy) ciopts(lc(navy navy) lw(thick thin))) ///
(est7, offset(-0.2) keep(*run*) rename(vs_run="Unease") mc(purple) ciopts(lc(purple purple) lw(thick thin))) ///
(est8, offset(0)    keep(*run*) rename(s_run="Unease") mc(green) ciopts(lc(green green) lw(thick thin))) ///
(est9, offset(0.2)  keep(*run*) rename(m_run="Unease") mc(navy) ciopts(lc(navy navy) lw(thick thin))) ///
(est10, offset(-0.2) keep(*run*) rename(vs_run="Fear") mc(purple) ciopts(lc(purple purple) lw(thick thin))) ///
(est11, offset(0)    keep(*run*) rename(s_run="Fear") mc(green) ciopts(lc(green green) lw(thick thin))) ///
(est12, offset(0.2)  keep(*run*) rename(m_run="Fear") mc(navy) ciopts(lc(navy navy) lw(thick thin))) ///
(est13, offset(-0.2) keep(*run*) rename(vs_run="Index") mc(purple) ciopts(lc(purple purple) lw(thick thin))) ///
(est14, offset(0)    keep(*run*) rename(s_run="Index") mc(green) ciopts(lc(green green) lw(thick thin))) ///
(est15, offset(0.2)  keep(*run*) rename(m_run="Index") mc(navy) ciopts(lc(navy navy) lw(thick thin))) ///
, vert ytitle("Treatment effects") title("") ///
yline(0, lc(red)) ///
xlabel(,nogrid nogextend) ylabel(-0.1(0.1)0.5,nogrid nogextend) ///
levels(90 95) msize(large) ///
xline(4.5, lc(gs12) lp(solid) lw(medthick)) ///
legend(order(3 "Very short run" 6 "Short run" 9 "Medium run") pos(6) row(1) region(lwidth(none)))
gr export "$output/fig_3.png", as(png) width(4000) replace

}



********************************************************************************
// Section 3.2


qui { // Figure 4 : Regs by attack
	
foreach y of varl risk_terror01 emo_terror_bad {
	
	est clear
	foreach attack in "07jul2005" "30jun2007" "22may2013" {
		eststo: areg `y' vs_run if attack_group==td(`attack'), r absorb(gor)
		eststo: areg `y' s_run  if attack_group==td(`attack'), r absorb(gor)
		eststo: areg `y' m_run  if attack_group==td(`attack'), r absorb(gor)
	}

coefplot ///
(est1, offset(-0.2) keep(*run*) rename(vs_run="Attack 1") mc(purple) ciopts(lc(purple purple) lw(thick thin))) ///
(est2, offset(0) keep(*run*) rename(s_run="Attack 1") mc(green) ciopts(lc(green green) lw(thick thin))) ///
(est3, offset(0.2) keep(*run*) rename(m_run="Attack 1") mc(navy) ciopts(lc(navy navy) lw(thick thin))) ///
(est4, offset(-0.2) keep(*run*) rename(vs_run="Attack 2") mc(purple) ciopts(lc(purple purple) lw(thick thin))) ///
(est5, offset(0) keep(*run*) rename(s_run="Attack 2") mc(green) ciopts(lc(green green) lw(thick thin))) ///
(est6, offset(0.2) keep(*run*) rename(m_run="Attack 2") mc(navy) ciopts(lc(navy navy) lw(thick thin))) ///
(est7, offset(-0.2) keep(*run*) rename(vs_run="Attack 3") mc(purple) ciopts(lc(purple purple) lw(thick thin))) ///
(est8, offset(0) keep(*run*) rename(s_run="Attack 3") mc(green) ciopts(lc(green green) lw(thick thin))) ///
(est9, offset(0.2) keep(*run*) rename(m_run="Attack 3") mc(navy) ciopts(lc(navy navy) lw(thick thin))) ///	
, vert yline(0, lc(red)) ///
ytitle("") title("`: var lab `y''", color(black)) ///
xlabel(,nogrid nogextend) ylabel(-0.1(0.1)0.5, nogrid nogextend) ///
levels(90 95) msize(large) ///
legend(order(3 "Very short run" 6 "Short run" 9 "Medium run") pos(6) row(1)  region(lwidth(none))) name(`y', replace)

}

grc1leg risk_terror01 emo_terror_bad, rows(1) col(2) l1("Treatment effects") b1("Time frame") ring(4)
gr export "$output/fig_4.png", as(png) width(4000) replace 

}



********************************************************************************
// Section 3.3

use "$project/replication_data_2", clear



qui { // Figure 5 : Failed attacks

** Panel A
foreach y of varl risk_terror01 {

	est clear
	eststo: areg `y' vs_run, r absorb(attack_gor) cluster(attack_gor)
	eststo: areg `y' s_run, r absorb(attack_gor) cluster(attack_gor)
	eststo: areg `y' m_run, r absorb(attack_gor) cluster(attack_gor)

	coefplot ///
	(est1, keep(*run*) mc(purple) ciopts(lc(purple purple) lw(thick thin))) ///
	(est2, keep(*run*) mc(green) ciopts(lc(green green) lw(thick thin))) ///
	(est3, keep(*run*) mc(navy) ciopts(lc(navy navy) lw(thick thin))) ///
	, vert yline(0, lc(red)) ///
	ytitle("") xtitle("Time frame") title("`: var lab `y''", color(black)) ///
	xlabel(,nogrid nogextend) ylabel(-0.1(0.1)0.5, nogrid nogextend) ///
	plotregion(lc(gs8)) ///
	levels(90 95) msize(large) ///
	legend(off) name(`y', replace)
}


** Panel B
est clear
foreach y of varl emo_terror_1 emo_terror_3 emo_terror_5 emo_terror_7 emo_terror_bad {
foreach x in vs_run s_run m_run {

	eststo: areg `y' `x', r absorb(attack_gor) cluster(attack_gor)
}
}
 
coefplot ///
(est1, offset(-0.2) keep(*run*) rename(vs_run="Anger") mc(purple) ciopts(lc(purple purple) lw(thick thin))) ///
(est2, offset(0)    keep(*run*) rename(s_run="Anger") mc(green) ciopts(lc(green green) lw(thick thin))) ///
(est3, offset(0.2)  keep(*run*) rename(m_run="Anger") mc(navy) ciopts(lc(navy navy) lw(thick thin))) ///
(est4, offset(-0.2) keep(*run*) rename(vs_run="Disgust") mc(purple) ciopts(lc(purple purple) lw(thick thin))) ///
(est5, offset(0)    keep(*run*) rename(s_run="Disgust") mc(green) ciopts(lc(green green) lw(thick thin))) ///
(est6, offset(0.2)  keep(*run*) rename(m_run="Disgust") mc(navy) ciopts(lc(navy navy) lw(thick thin))) ///
(est7, offset(-0.2) keep(*run*) rename(vs_run="Unease") mc(purple) ciopts(lc(purple purple) lw(thick thin))) ///
(est8, offset(0)    keep(*run*) rename(s_run="Unease") mc(green) ciopts(lc(green green) lw(thick thin))) ///
(est9, offset(0.2)  keep(*run*) rename(m_run="Unease") mc(navy) ciopts(lc(navy navy) lw(thick thin))) ///
(est10, offset(-0.2) keep(*run*) rename(vs_run="Fear") mc(purple) ciopts(lc(purple purple) lw(thick thin))) ///
(est11, offset(0)    keep(*run*) rename(s_run="Fear") mc(green) ciopts(lc(green green) lw(thick thin))) ///
(est12, offset(0.2)  keep(*run*) rename(m_run="Fear") mc(navy) ciopts(lc(navy navy) lw(thick thin))) ///
(est13, offset(-0.2) keep(*run*) rename(vs_run="Index") mc(purple) ciopts(lc(purple purple) lw(thick thin))) ///
(est14, offset(0)    keep(*run*) rename(s_run="Index") mc(green) ciopts(lc(green green) lw(thick thin))) ///
(est15, offset(0.2)  keep(*run*) rename(m_run="Index") mc(navy) ciopts(lc(navy navy) lw(thick thin))) ///
, vert ytitle("") xtitle("Emotion") title("Negative emotions", color(black)) ///
yline(0, lc(red)) ///
xlabel(,nogrid nogextend) ylabel(-0.1(0.1)0.5, nogrid nogextend) ///
plotregion(lc(gs8)) ///
levels(90 95) msize(large) ///
xline(4.5, lc(gs12) lp(solid) lw(medthick)) ///
legend(off) ///
name(emo_terror, replace)
	
gr combine risk_terror01 emo_terror, rows(1) col(2) l1("Treatment effects") b1("") 
gr export "$output/fig_5.png", as(png) width(4000) replace 


}

